package com.jeff.tianti.controller;

import com.jeff.tianti.cms.entity.Media;
import com.jeff.tianti.cms.service.MediaService;
import com.jeff.tianti.common.PageBean;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.BaseEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping("/media")//媒体供需
public class MediaController {
    @Autowired
    private MediaService mediaService;

    /**
     * @Ceator:liuYI
     * @Describe:首页媒体供需模块，按照更新时间排序取最新两条并做分页
     */
    @RequestMapping(value = "/home/page", method = RequestMethod.POST)
    public AjaxResult homePageMedia(@RequestBody PageBean pageBean) {
        Pageable pageable = new PageRequest(pageBean.getCurrentPage(), pageBean.getPageSize());
        List<Media> medias = mediaService.findAllNoDel(pageable);
        return AjaxResult.setSuccessData(medias);
    }

    /**
     * @Ceator:liuYI
     * @Describe:首页媒体供需模块详情查看
     */
    @RequestMapping(value = "/details", method = RequestMethod.GET)
    public AjaxResult findById(@RequestParam("id") String id) {
        if (StringUtils.isEmpty(id)) {
            throw new RuntimeException("id:参数为空");
        }
        Media media = mediaService.find(id);
        return AjaxResult.setSuccessData(media);
    }

    /**
     * @Ceator:liuYI
     * @Describe:个人中心_媒体供需_发布媒体/修改
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxResult saveMedia(@RequestBody Media media) {
        String id = media.getId();
        List<String> banners = media.getBanners();
        String substring = "";
        if (!CollectionUtils.isEmpty(banners)) {
            String str = banners.toString();
            substring = str.substring(1, str.length() - 1);
        }
        media.setBanner(substring);
        if (!StringUtils.isEmpty(id)) {//修改
            media.setAuditFlag(BaseEntity.AUDIT_FLAG_NOT);
            mediaService.update(media);
        } else {//新增
            media.setAuditFlag(BaseEntity.AUDIT_FLAG_NOT);
            mediaService.save(media);
        }
        return AjaxResult.setSuccessData(null);
    }

    /**
     * @Ceator:liuYI
     * @Describe:个人中心_媒体供需_用户删除媒体供需
     */
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    public AjaxResult deleteMedia(@RequestParam("id") String id, @RequestParam("uid") String uid) {
        if (StringUtils.isBlank(uid)) {
            throw new RuntimeException("uid:参数缺失");
        }
        Media media = mediaService.find(id);
        if (!uid.equals(media.getUid())) {
            throw new RuntimeException("非法操作");
        }
        mediaService.delete(id);
        return AjaxResult.setSuccessData(null);
    }
}
